import 'package:flutter/material.dart';

enum PlaceholderPageType {
  notData,
  loading,
}

Widget showPlaceholderPage(
    {required bool show,
    PlaceholderPageType type = PlaceholderPageType.notData,
    String? placeholder,
    required Widget child}) {
  return show ? PlaceholderPage(type: type, placeholder: placeholder) : child;
}

class PlaceholderPage extends StatelessWidget {
  final PlaceholderPageType type;
  final String? placeholder;

  const PlaceholderPage({super.key, required this.type, this.placeholder});

  Widget get _buildWidget {
    switch (type) {
      case PlaceholderPageType.notData:
        return _notDataWidget;
      case PlaceholderPageType.loading:
        return _loadingWidget;
    }
  }

  @override
  Widget build(BuildContext context) {
    return SizedBox(
        width: double.infinity, height: double.infinity, child: _buildWidget);
  }

  Widget get _notDataWidget => SizedBox(
        width: double.infinity,
        height: double.infinity,
        child: Center(
          child: Text(placeholder ?? '无数据'),
        ),
      );

  Widget get _loadingWidget => Container(
        width: double.infinity,
        height: double.infinity,
        decoration: const BoxDecoration(color: Colors.transparent),
        alignment: Alignment.center,
        child: Container(
          height: 80,
          padding: const EdgeInsets.all(10),
          decoration: BoxDecoration(
              color: const Color(0x88000000),
              borderRadius: BorderRadius.circular(6)),
          child: const Column(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: <Widget>[CircularProgressIndicator(), Text('正在加载')],
          ),
        ),
      );
}
